package com.nuoniu.sibanyun.controller.crm;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmUserSearch;
import com.nuoniu.sibanyun.service.crm.ICrmUserSearchService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 查询视图 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-20
 */
@RestController
@RequestMapping("/crm/userSearch")
@Api(tags = "客户用户查询管理")
public class CrmUserSearchController extends BaseController {

    @Autowired
    private ICrmUserSearchService searchService;

    /**
     * 获取用户查询
     * @author 小啊刚
     * @date 2020/6/23
     */
    @GetMapping("detail")
    public HttpResult getUserSearch(Integer searchType) {
        if(searchType == null) {
            throw new NuoNiuException("类型不能为空");
        }
        return HttpResult.ok(searchService.getUserSearch(searchType));
    }

    /**
     * 保存用户查询
     * @author 小啊刚
     * @date 2020/6/23
     */
    @PostMapping("save")
    public HttpResult saveUserSearch(@RequestBody CrmUserSearch search) {
        if(search.getSearchType() != null && StringUtils.isNotBlank(search.getSearchVal())) {
            LocalDateTime now = LocalDateTime.now();
            UserInfo userInfo = UserThreadLocal.get();
            if(null == search.getId()){
                search.setCreateUserId(userInfo.getUserId());
                search.setCreateDate(now);
            }
            search.setUpdateUserId(userInfo.getUserId());
            search.setUpdateDate(now);
            searchService.saveOrUpdate(search);
            return HttpResult.ok();
        }else{
            throw new NuoNiuException("保存失败");
        }

    }

    /**
     * 删除用户常用查询
     * @param id
     * @return
     */
    @DeleteMapping("delete")
    public HttpResult delete(Integer id){
        searchService.deleteOne(id);
        return HttpResult.ok();
    }
}
